<template>
  <div class="about">
    <header>
      购物清单
    </header>
    <ul>
      <li v-for="(item,index) in cartList" :key="index">
        <input type="checkbox" v-model="item.check" @change="checkOne">
        <img src="../assets/img.png" alt="">
         <section>
           <h4>{{item.name}}</h4>
           <p class="price">${{item.price}}</p>
         </section>
         <div>
           <p @click="item.num++">+</p>
           <p>{{item.num}}</p>
           <p @click="item.num==1?1:item.num--">-</p>
         </div>
      </li>
    </ul>
    <div class="foot">
      <div> 
        <input type="checkbox" v-model="checkAll" @change="allCheck(checkAll)">
        <span @click.stop="delect" style="margin-left:10px">删除所选</span>
      </div>
      <span>
         合计：{{allPrice}}
      </span>
      <span>
        去结算（{{allNum}}）
      </span>
    </div>
  </div>
</template>
<script>
import { mapState,mapMutations } from 'vuex'
export default {
  data(){
    return{
      checkAll: false
    }
  },
  computed:{
    ...mapState(['cartList']),
    allPrice(){ // 总价
      let price = 0;
      this.cartList.forEach(item => {
         if (item.check){
           price+= item.price*item.num
         }
      })
      return price
    },
    allNum(){ // 总价
      let num = 0;
      this.cartList.forEach(item => {
         if (item.check){
           num+=item.num
         }
      })
      return num
    },
  },
  methods: {
    ...mapMutations(['allCheck','delect']),
    checkOne(){ // 反选的事件
      this.checkAll = this.cartList.every(item => item.check);
    }
    // checkftn(){ // 全选的事件
    //   console.log(this.checkAll);
    // }
  },
}
</script>
<style lang="scss" scoped>
  // .about{
  //   height: 100%;
  // }
  header{
    height: 45px;
    line-height: 45px;
    text-align: center;
  }
  ul{
    // vh 视图窗口的高度
    height: calc(100vh - 95px);
    overflow: auto;
  }
  li{
    display: flex;
    padding: 0 10px;
    align-items: center;
    .price{
      margin-top: 5px;
      color: #8d3ec5;
    }
    section{
      flex: 1;
    }
  }
  .foot{
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span:last-child{
      background:  #440f7d;
      color: #fff;
      line-height: 50px;
      padding: 0 10px;
    }
    label{
      margin-left: 10px;
    }
  }
</style>
